package no.group.gowther.util;

import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

/**
 * @author ayatsuji
 * @date 2022-05-03 22:33
 * @since
 */
public record FilePathGenerator(Path filePath) {
    public Path generate() {
        if (!Files.exists(filePath)) return filePath;
        final String theFilePath = filePath.toString();
        final int dotIndex = theFilePath.lastIndexOf(".");
        final String extension = theFilePath.substring(dotIndex);
        final String filenameWithoutExt = theFilePath.substring(0, dotIndex);
        Path newFilePath;
        int i = 0;
        do {
            newFilePath = Paths.get(filenameWithoutExt + "_" + i++ + extension);
        } while (Files.exists(newFilePath));
        return newFilePath;
    }


}
